const { app, BrowserWindow, ipcMain } = require("electron/main");
// require("electron-reload")(__dirname);
const path = require("path");
const url = require("url");

app.whenReady().then(() => {
  const win = new BrowserWindow({
    width: 600,
    height: 600,
    webPreferences: {
      nodeIntegration: true, // 允许在渲染进程（窗口）中使用node.js
      contextIsolation: false, // 关闭上下文隔离
    },
  });
  win.loadURL(
    url.format({
      protocol: "file",
      pathname: path.join(__dirname, "window/window.html"),
    })
  ); // 必须这么整，要不然window会从根目录开始找);
  // win.webContents.openDevTools();
});

ipcMain.on("port", (event) => {
  const port2 = event.ports[0];
  port2.on("message", (event) => {
    console.log(event.data); // 读取渲染进程发来的数据
    // 主进程再给渲染进程传递一些消息
    port2.postMessage("主进程：我收到消息了，我们周日见！");
  });
  port2.start(); // 开启端口
});
